package com.glwan.activitytest_2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    private EditText mEditName;
    private EditText mEditPassword;
    private Button mBtnLogin;
    private Button mBtnRegister;
    private ActivityResultLauncher mActivityResultLauncher;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mEditName = findViewById(R.id.name);
        mEditPassword = findViewById(R.id.password);
        mBtnLogin = findViewById(R.id.login);
        mBtnRegister = findViewById(R.id.regist);
        init();
        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegeistActivity.class);
                //mActivityResultLauncher.launch(intent);
                startActivity(intent);
            }
        });
    }

    private void init() {
        mActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == RESULT_OK) {
                            Intent intent = result.getData();
                            String name = intent.getStringExtra("name");
                            String password = intent.getStringExtra("password");
                            mEditName.setText(name);
                            mEditPassword.setText(password);
                        }
                    }
                });
    }
}
